package cn.stylefeng.guns.modular.order.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.order.entity.OrderWeightCheck;
import cn.stylefeng.guns.modular.order.model.params.OrderWeightCheckParam;
import cn.stylefeng.guns.modular.order.model.result.OrderWeightCheckResult;
import cn.stylefeng.guns.modular.order.service.OrderWeightCheckService;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.apache.commons.io.IOUtils;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.List;


/**
 * 订单重量复核控制器
 *
 * @author zhangzh
 * @Date 2024-05-20 14:30:07
 */
@Controller
@RequestMapping("/orderWeightCheck")
public class OrderWeightCheckController extends BaseController {

    private String PREFIX = "/order/orderWeightCheck";

    @Autowired
    private OrderWeightCheckService orderWeightCheckService;

    @Autowired
    private UserService userService;

    /**
     * 跳转到主页面
     *
     * @author zhangzh
     * @Date 2024-05-20
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/orderWeightCheck.html";
    }

    /**
     * 新增页面
     *
     * @author zhangzh
     * @Date 2024-05-20
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/orderWeightCheck_add.html";
    }

    /**
     * 编辑页面
     *
     * @author zhangzh
     * @Date 2024-05-20
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/orderWeightCheck_edit.html";
    }

    /**
     * 新增接口
     *
     * @author zhangzh
     * @Date 2024-05-20
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(OrderWeightCheckParam orderWeightCheckParam) {
        User user = userService.getcurrentUser();
        orderWeightCheckParam.setDeliverId(user.getDeliverId());
        orderWeightCheckParam.setDeptId(user.getDeptId());
        orderWeightCheckParam.setCreateUser(user.getUserId());
        orderWeightCheckParam.setCreateTime(new Date());
        this.orderWeightCheckService.add(orderWeightCheckParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author zhangzh
     * @Date 2024-05-20
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(OrderWeightCheckParam orderWeightCheckParam) {
        User user = userService.getcurrentUser();
        orderWeightCheckParam.setUpdateUser(user.getUserId());
        this.orderWeightCheckService.update(orderWeightCheckParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author zhangzh
     * @Date 2024-05-20
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(OrderWeightCheckParam orderWeightCheckParam) {
        this.orderWeightCheckService.delete(orderWeightCheckParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author zhangzh
     * @Date 2024-05-20
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(OrderWeightCheckParam orderWeightCheckParam) {
        OrderWeightCheck detail = this.orderWeightCheckService.getById(orderWeightCheckParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author zhangzh
     * @Date 2024-05-20
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(OrderWeightCheckParam orderWeightCheckParam,String condition ) {
        return this.orderWeightCheckService.findPageBySpec(orderWeightCheckParam,condition);
    }

    @ResponseBody
    @RequestMapping("/listAll")
    public List<OrderWeightCheckResult> listAll(OrderWeightCheckParam orderWeightCheckParam ) {
        return this.orderWeightCheckService.findListBySpec(orderWeightCheckParam);
    }


    /**
     * 导入页面
     */
    @RequestMapping("/toimportExcel")
    public String toimportExcel() {
        return PREFIX + "/import.html";
    }

    /**
     * 下载模板
     *
     * @throws ServletException , IOException
     */
    @RequestMapping("downloadExcel")
    public void downloadExcel(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String path = "assets/excel/orderWeight.xlsx";
        try {
            InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(path);
            OutputStream outputStream = response.getOutputStream();
            //设置下载的文件类型
            response.setContentType("application/vnd.ms-excel;charset=utf-8");
            //设置下载文件名
            response.setHeader("Content-Disposition", "attachment;filename=" + new String(("订单重量" + ".xlsx").getBytes(), "iso-8859-1"));
            IOUtils.copy(inputStream, outputStream);
            outputStream.flush();
            inputStream.close();
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 导入excel
     */
    @RequestMapping(value = "/importExcel", method = {RequestMethod.POST})
    @ResponseBody
    public ResponseData importExceNewmethodl(@RequestPart("file") MultipartFile file, OrderWeightCheckParam orderWeightCheckParam) {
        ResponseData data = new ResponseData();
        User user = userService.getcurrentUser();
        orderWeightCheckParam.setDeliverId(user.getDeliverId());
        orderWeightCheckParam.setDeptId(user.getDeptId());
        orderWeightCheckParam.setCreateTime(new Date());
        orderWeightCheckParam.setCreateUser(user.getUserId());
        orderWeightCheckParam.setUpdateUser(user.getUserId());
        try {
            data = orderWeightCheckService.getExceldata(file, orderWeightCheckParam, data);//处理导入数据
        } catch (Exception e) {
            data.setSuccess(false);
            e.printStackTrace();
        }
        return data;
    }
}


